import urllib.request
import urllib.parse


# https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20
def create_url(page):
    url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&'

    data = {
        'start':(page-1)*20,
        'limit':20
    }
    data = urllib.parse.urlencode(data)
    url = url + data
    print(url)
    return url

def create_request(url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'}
    reques = urllib.request.Request(url = url,headers = headers)
    open = urllib.request.urlopen(reques)
    com = open.read().decode('utf-8')
    return com

def create_file(com,page):
    df = open('../爬取的文件/douban_'+str(page)+'.json','w',encoding='utf-8')
    df.write(com)




if __name__ == '__main__':
    start_page = int(input('请输入开始页数：'))
    end_page = int(input('请输入结束页数：'))

    for page in range(start_page,end_page+1):
        print(page)

        url = create_url(page)
        com = create_request(url)
        create_file(com,page)